#include "audio.h"

audio::audio(QObject *parent) : QObject(parent)
{

}
void audio::startAudio(QString fileName)
{
    QAudioDeviceInfo device = QAudioDeviceInfo::defaultInputDevice();
    if(device.isNull())
    {
        QMessageBox::warning(NULL,"QAudioDeviceInfo","录音设备不存在");
    }
    else
    {
        QAudioFormat my_Format;
        //设置采样频率
        my_Format.setSampleRate(16000);
        //设置声道数
        my_Format.setChannelCount(1);
        //设置位深
        my_Format.setSampleSize(16);
        //设置编码
        my_Format.setCodec("audio/pcm");
        //判断该格式是否支持
        if(!device.isFormatSupported(my_Format))
        {
            my_Format = device.nearestFormat(my_Format);
        }

        //打开文件
        my_file = new QFile;
        my_file->setFileName(fileName);
        my_file->open(QIODevice::WriteOnly);
        //创建录音对象
        my_Audio = new QAudioInput(my_Format,this);
        my_Audio->start(my_file);
     }

}
void audio::stopAudio()
{
    //录音停止
    my_Audio->stop();
    //关闭文件
    my_file->close();
    //删除文件对象
    delete my_file;
    my_file = NULL;
}
